package com.ljl.org.service.impl;

import com.ljl.basic.service.impl.BaseServiceImpl;
import com.ljl.org.domain.Department;
import com.ljl.org.mapper.DepartmentMapper;
import com.ljl.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 部门服务实现类
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
        if (department.getParent() == null) {
            // 如果当前部门没有选择部门
            department.setDirPath("/" + department.getId());
        } else {
            // 如果选择了部门
            // 先查询上级部门的id
            Long parent_id = department.getParent().getId();
            // 通过上级部门的id查询上级部门的dirPath
            String parentPath = departmentMapper.findById(parent_id).getDirPath();
            // 将上级部门的dirPath和自己的id拼接成自己的dirPath
            department.setDirPath(parentPath + "/" + department.getId());
        }
        this.update(department);

    }

    @Transactional
    @Override
    public void update(Department department) {
        if (department.getParent() == null) {
            // 如果当前部门没有选择部门
            department.setDirPath("/" + department.getId());
        } else {
            // 如果选择了部门
            // 先查询上级部门的id
            Long parent_id = department.getParent().getId();
            // 通过上级部门的id查询上级部门的dirPath
            String parentPath = departmentMapper.findById(parent_id).getDirPath();
            // 将上级部门的dirPath和自己的id拼接成自己的dirPath
            department.setDirPath(parentPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> deptTree() {
        // 查询所有的部门
        List<Department> depts = departmentMapper.findAll();
        // 创建一个Map用来装查询出来的部门
        HashMap<Long, Department> map = new HashMap<>();
        // 循环将depts中的数据装入map中，可以通过id定位到对象
        for (Department dept : depts) {
            map.put(dept.getId(),dept);
        }
        ArrayList<Department> deptTree = new ArrayList<>();
        // 循环depts
        for (Department dept : depts) {
            // 如果没有上级部门
            if (dept.getParent_id() == null) {
                // 直接将dept放入
                deptTree.add(dept);
            } else {
                // 有上级部门c
                Long parent_id = dept.getParent_id();
                // 通过上级部门的id获取上级部门对象
                Department department = map.get(parent_id);
                // 将自己添加到上级部门的children中去
                department.getChildren().add(dept);
            }
        }
        return deptTree;
    }

}
